﻿using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.Infrastructure.Interfaces;
using B.S.Meeting.WriteAPI.Applications.Commands.MeetingRooms;
using MediatR;

namespace B.S.Meeting.WriteAPI.Applications.CommandHandlers.MeetingRooms
{
    public class BatchDelRoomCommandHandler : IRequestHandler<BatchDelRoomCommand, APIResult<int>>
    {
        IBaseRepository<MeetingRoom> mrRep;
        ILogger<BatchDelRoomCommandHandler> logger;
        public BatchDelRoomCommandHandler(IBaseRepository<MeetingRoom> mrRep, ILogger<BatchDelRoomCommandHandler> logger)
        {
            this.mrRep = mrRep;
            this.logger = logger;
        }
        public async Task<APIResult<int>> Handle(BatchDelRoomCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list=mrRep.GetAll().Where(x => request.MeetingRoomIds.Contains(x.MeetingRoomId) && x.IsDel == false).ToList();
                if (list != null && list.Count() > 0)
                {
                    foreach (var item in list)
                    {
                        item.IsDel = true; // 逻辑删除
                        item.UpdateTime = DateTime.Now; // 更新时间
                        await mrRep.UpdAsync(item); // 在循环内部调用 UpdAsync
                    }
                    return new APIResult<int>
                    {
                        Code = StatusEnumCode.Ok,
                        Message = "批量删除会议室成功",
                        Data = list.Count() // 或者返回受影响的总行数，取决于 BaseRepository 的实现
                    };
                }
                return new APIResult<int>
                {
                     Code = StatusEnumCode.Fail,
                     Message = "未找到要删除的会议室信息",
                     Data = 0
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
